package com.ruoyi.plus.utils;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.file.FileNameLengthLimitExceededException;
import com.ruoyi.common.exception.file.FileSizeLimitExceededException;
import com.ruoyi.common.exception.file.InvalidExtensionException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.plus.config.ContextPathConfig;
import com.ruoyi.plus.utils.aliyunoss.AliyunOssUtils;
import com.ruoyi.plus.utils.minio.MinIoUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.Objects;

public class FileUploadUtilsPlus extends FileUploadUtils {


    /**
     * 根据文件路径上传
     *
     * @param baseDir 相对应用的基目录
     * @param file    上传的文件
     * @return 文件名称
     * @throws IOException
     */
    public static final String upload(String baseDir, MultipartFile file, boolean keepFileName) throws IOException {
        try {
            return upload(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION, keepFileName);
        } catch (Exception e) {
            throw new IOException(e.getMessage(), e);
        }
    }

    /**
     * 文件上传
     *
     * @param baseDir          相对应用的基目录
     * @param file             上传的文件
     * @param allowedExtension 上传文件类型
     * @return 返回上传成功的文件名
     * @throws FileSizeLimitExceededException       如果超出最大大小
     * @throws FileNameLengthLimitExceededException 文件名太长
     * @throws IOException                          比如读写文件出错时
     * @throws InvalidExtensionException            文件校验异常
     */
    public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension, boolean keepFileName)
            throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,
            InvalidExtensionException {
        assertAllowed(file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
        if (MinIoUtils.config != null && MinIoUtils.config.isEnabled()) {
            baseDir = ("upload/" + baseDir + "/").replace("//", "/");
            return MinIoUtils.uploadFile(file, baseDir + extractFilename(file, keepFileName));
        } else if (AliyunOssUtils.config != null && AliyunOssUtils.config.isEnabled()) {
            return AliyunOssUtils.uploadFile(file, baseDir + extractFilename(file, keepFileName));
        } else {
            int fileNamelength = Objects.requireNonNull(file.getOriginalFilename()).length();
            if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH) {
                throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
            }

            assertAllowed(file, allowedExtension);

            String fileName = extractFilename(file, keepFileName);

            String absPath = getAbsoluteFile(baseDir, fileName).getAbsolutePath();
            file.transferTo(Paths.get(absPath));
            return getPathFileName(baseDir, fileName);
        }
    }

    /**
     * 编码文件名
     */
    public static final String extractFilename(MultipartFile file, boolean keepFileName) {
        if (keepFileName) {
            return file.getOriginalFilename();
        }
        return extractFilename(file);
    }

}
